<?php
namespace app\login\controller;

error_reporting(E_ALL ^ E_NOTICE);
use app\XDeode;
use think\Db;
use think\Controller;
class Alipay extends Controller
{
    public function pay(){
        require_once APP_PATH.'../'.'extend/alipay/wappay/service/AlipayTradeService.php';
        require_once APP_PATH.'../'.'extend/alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
        require_once APP_PATH.'../'.'extend/alipay/config.php';
        $_var_482 = input();
    $orderNo = md5(uniqid());//$_var_482['order_no'];//
//     if(!$orderNo){
// 		return json(['code' => 0, 'msg' => '订单号不能为空']);
//     }
        //商户订单号，商户网站订单系统中唯一订单号，必填
    $out_trade_no = $orderNo;//$_var_482['WIDout_trade_no'];
    $userId = $_var_482['user_id'];
    if(!$userId){
		return json(['code' => 0, 'msg' => '用户ID不能为空']);
    }
    //订单名称，必填
    $subject = $_var_482['title'];
    if(!$subject){
		return json(['code' => 0, 'msg' => '订单名称不能为空']);
    }
    //付款金额，必填
    $total_amount =$_var_482['amount'];
    if($total_amount<=0){
		return json(['code' => 0, 'msg' => '订单金额不合法']);
    }
//     $productId = $_var_482['product_id'];
//     if(!$productId){
// 		return json(['code' => 0, 'msg' => '产品id不能为空']);
//     }
    $name = $_var_482['name'];
    if(!$name){
		return json(['code' => 0, 'msg' => 'vip名称不能为空']);
    }
    //商品描述，可空
    $body = $_var_482['desc'];
    $data['order_no'] = $orderNo;
    $data['money'] = $total_amount;
    $data['user_id'] = $userId;
    $data['title'] = $subject;
    $data['pay_type'] = 1;
    $data['status'] = 1;
    $data['add_time'] = time();
    $data['product_id'] = 0;//$productId;
    $data['name'] = $name;
    $exist = db('order')->where('order_no="'.$orderNo.'"')->find();
    if($exist){
        return json(['code' => 0, 'msg' => '订单异常']);
    }
    db('order')->insert($data);
    
    //超时时间
    $timeout_express="1m";

    $payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
    $payRequestBuilder->setBody($body);
    $payRequestBuilder->setSubject($subject);
    $payRequestBuilder->setOutTradeNo($out_trade_no);
    $payRequestBuilder->setTotalAmount($total_amount);
    $payRequestBuilder->setTimeExpress($timeout_express);

    $payResponse = new \AlipayTradeService($config);
    $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);
    
    return;
        
    }
    
}